package com.campus.dao.impl;

import com.campus.dao.ActivityDao;
import com.campus.model.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @desc: 活动数据访问层实现类
 * @author: hjm
 * @date: 2024/11/12 14.41
 */
@Repository
public class ActivityDaoImpl implements ActivityDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 创建活动
     *
     * @param activity 活动对象
     * @return 插入记录的行数
     */
    @Override
    public int createActivity(Activity activity) {
        String sql = "INSERT INTO t_manager_activity " +
                "(name, category, title, creator, abstract, detailed_desc, status, created_time, start_time, end_time, pic_urls) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] params = {
                activity.getName(),
                activity.getCategory(),
                activity.getTitle(),
                activity.getCreator(),
                activity.getActivityAbstract(),
                activity.getDetailedDesc(),
                activity.getStatus(),
                activity.getCreatedTime(),
                activity.getStartTime(),
                activity.getEndTime(),
                activity.getPicUrls()
        };
        return jdbcTemplate.update(sql, params);
    }

    /**
     * 更新活动
     *
     * @param activity 活动对象
     * @return 更新记录的行数
     */
    @Override
    public int updateActivity(Activity activity) {
        String sql = "UPDATE t_manager_activity SET " +
                "name = ?, category = ?, title = ?, creator = ?, abstract = ?, detailed_desc = ?, status = ?, start_time = ?, end_time = ?, pic_urls = ? " +
                "WHERE id = ?";
        Object[] params = {
                activity.getName(),
                activity.getCategory(),
                activity.getTitle(),
                activity.getCreator(),
                activity.getActivityAbstract(),
                activity.getDetailedDesc(),
                activity.getStatus(),
                activity.getStartTime(),
                activity.getEndTime(),
                activity.getPicUrls(),
                activity.getId()
        };
        return jdbcTemplate.update(sql, params);
    }

    /**
     * 发布活动
     *
     * @param id 活动ID
     * @return 更新记录的行数
     */
    @Override
    public int publishActivity(int id) {
        String sql = "UPDATE t_manager_activity SET status = 1 WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    /**
     * 查询活动列表
     *
     * @param status      活动状态
     * @param nameKeyword 活动名称关键词
     * @param sortBy      排序字段
     * @return 活动列表
     */
    @Override
    public List<Activity> queryActivities(String status, String nameKeyword, String sortBy) {
        StringBuilder sql = new StringBuilder("SELECT * FROM t_manager_activity WHERE 1=1");

        // 动态拼接查询条件
        if (status != null && !status.isEmpty()) {
            sql.append(" AND status = ").append(status);
        }
        if (nameKeyword != null && !nameKeyword.isEmpty()) {
            sql.append(" AND name LIKE '%").append(nameKeyword).append("%'");
        }

        // 动态排序
        sql.append(" ORDER BY ").append(sortBy != null ? sortBy : "created_time").append(" DESC");

        return jdbcTemplate.query(sql.toString(),
                (rs, rowNum) -> {
                    Activity activity = new Activity();
                    activity.setId((long) rs.getInt("id"));
                    activity.setName(rs.getString("name"));
                    activity.setCategory(rs.getString("category"));
                    activity.setTitle(rs.getString("title"));
                    activity.setCreator(rs.getString("creator"));
                    activity.setActivityAbstract(rs.getString("abstract"));
                    activity.setDetailedDesc(rs.getString("detailed_desc"));
                    activity.setStatus((int) rs.getInt("status"));
                    activity.setCreatedTime(rs.getTimestamp("created_time"));
                    activity.setStartTime(rs.getTimestamp("start_time"));
                    activity.setEndTime(rs.getTimestamp("end_time"));
                    activity.setPicUrls(rs.getString("pic_urls"));
                    return activity;
                }
        );
    }
}